package controllers.buy;

import models.Product;
import play.mvc.Controller;
import play.mvc.Result;
import controllers.routes;

public class CartController extends Controller {

	public static Result list() {

		return ok(views.html.cart.list.render(getUserCart(), getUserCart()
				.getSummary()));
	}

	public static Result create(Product p, Integer amount) {
		getUserCart().save(p, amount);
		return redirect(controllers.buy.routes.CartController.list());
	}

	private static Cart getUserCart() {
		return Cart.getUserCart(session("currentUserEmail"));
	}

	public static Result remove(Integer id) {
		if (getUserCart().size() > id && id >= 0) {
			if (null != getUserCart().remove(id.intValue())) {
				return list();
			}
			return redirect(routes.Application.index());
		}
		return redirect(routes.Application.index());
	}
}
